<template>
  <div>
    <div class="header">
      <div class="same">
        <div class="hed">
          <div class="i">
            <i
              class="el-icon-arrow-left"
              @click="$router.push('/ShopCart')"
            ></i>
            <i class="iconfont icon-home" @click="$router.replace('/')"></i>
          </div>
          <p>提交订单</p>
        </div>
      </div>
    </div>
    <div class="main">
      <van-contact-card type="edit" :tel="tel" :name="name" @click="onEdit" />
      <div class="same">
        <div class="til">
          <p>萌宠乐园</p>
        </div>
        <div class="list">
          <div class="shop" v-for="item in list" :key="item.id">
            <img :src="`http://192.168.10.23:8888/` + item.listUrl" alt="" />
            <dl>
              <dt>{{ item.listName }}</dt>
              <dd>
                ￥{{ item.listPrice }}<span>数量：{{ item.listCount }}</span>
              </dd>
            </dl>
          </div>
        </div>
      </div>
      <div class="footer">
        <div class="same">
          <div class="price">
            <p>商品总价</p>
            <p>￥{{ totals }}.00</p>
          </div>
        </div>
        <van-submit-bar
          :price="totals * 100"
          button-text="提交订单"
          @submit="onSubmit"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { AddOrders, ShopCartDel } from "@/api/index";
import { AddressList } from "@/api/address";
export default {
  components: {},
  data() {
    return {
      tel: "",
      name: "",
      list: JSON.parse(localStorage.getItem("list"))
        ? JSON.parse(localStorage.getItem("list"))
        : [],
      totals: localStorage.getItem("totals")
        ? localStorage.getItem("totals")
        : 0,
      address: "辽宁省沈阳市浑南区东大自动化大厦",
    };
  },
  created() {},

  mounted() {
    // this.list = this.$route.query.list;
    // this.totals = this.$route.query.totals;
    // console.log(this.list);
    if (this.$route.query.name == undefined) {
      this.AddressList();
    } else {
      this.tel = this.$route.query.tel;
      this.name = this.$route.query.name;
    }
  },

  methods: {
    AddressList() {
      let userId = this.$store.state.user.id;
      AddressList(userId).then((res) => {
        // console.log(res, "address");
        this.address = res.data.data.filter(
          (item) => item.isDefault == true
        )[0];
        this.name = this.address.name;
        this.tel = this.address.telephone;
      });
    },
    onSubmit() {
      let userId = this.$store.state.user.id;
      let order = {
        usersId: userId,
        tel: this.tel,
        name: this.name,
        totals: Number(this.totals),
        address: this.address,
        list: this.list,
      };

      // console.log(this.$route.query.address);

      AddOrders(order).then((res) => {
        // console.log(res);

        if (res.data.code === 10000) {
          this.$router.push("/Order/0");
          localStorage.removeItem("list");
          localStorage.removeItem("totals");
          // setTimeout(() => {
          //   let arr = this.list.map((item) => item.id);
          //   // console.log(arr);
          //    for (let i = 0; i < arr.length; i++) {
          //     ShopCartDel(arr[i]).then((res) => {
          //       console.log(res, "删除");
          //     });
          //   }

          // }, 5000);
        }
      });
    },
    onEdit(valias) {
      this.$router.push("/OrdersAddress");
    },
  },
};
</script>
<style scoped lang="scss">
.header {
  width: 100%;
  height: 8vh;
  z-index: 5;
  top: 0;
  position: sticky;

  background: transparent;
  background: rgb(255, 255, 255);
  .hed {
    width: 100%;
    height: 8vh;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 20px;
    .i {
      display: flex;
      align-items: center;
      i {
        margin: 0 2vw;
      }
      .icon-home {
        font-size: 22px;
      }
    }
    p {
      width: 58vw;
    }
  }
}
.main {
  width: 100%;
  height: 50vh;
  // background: red;
  color: #333;
  .footer {
    width: 100%;
    height: 20vh;
    margin-top: 5vh;
    background: rgb(255, 255, 255);

    .price {
      height: 10vh;
      display: flex;

      align-items: center;
      justify-content: space-between;
    }
  }
  .til {
    width: 100%;
    height: 8vh;
    align-items: center;
    border-bottom: 1px solid #ccc;
    display: flex;
  }
  .list {
    width: 100%;
    height: auto;
    .shop {
      width: 100%;
      height: 15vh;
      background: rgb(255, 255, 255);
      margin-top: 2vh;
      display: flex;

      img {
        height: 15vh;
      }
      dl {
        width: 70%;
        font-size: 16px;
        margin-left: 2vw;
        color: #333;
        dd {
          font-size: 14px;
          color: rgb(240, 60, 15);
          span {
            color: #333;
            margin-left: 2vw;
          }
        }
      }
    }
  }
}
</style>
